﻿'========================================================================
'功能:
'作者:andy
'日期:
'========================================================================
Imports System.Data
Partial Class WebForm_AssetMaintain
    Inherits BasePage
    Const intMaintain As Integer = 0
    Const intEliminate As Integer = 1
    Const intTransfer As Integer = 2
    Const intFlower As Integer = 3
    Const intReply As Integer = 4
    Const intSOP As Integer = 5


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            hiUID.Value = ActualUID
            hiDBName.Value = dbName
            ddlasset_status.DataBind()
            Dim bbl As OAMS.BBL.GroupBBL = New OAMS.BBL.GroupBBL()


            btnClick(intMaintain)
            If ChkProgramRight("3") = False Then
                btnApply.Visible = False

            End If
            If ChkProgramRight("4") = False Then
                btnReply.Visible = False

            End If
            If ChkProgramRight("5") = False Then
                btnSOP.Visible = False

            End If
            If ChkProgramRight("6") = False Then
                btnInsert.Visible = False

            End If


            If bbl.GroupMemberIsExist("Admin", hiUID.Value, hiDBName.Value) Then
                txtowner.Text = hiUID.Value
                txtowner.ReadOnly = False
            Else

                txtowner.Text = hiUID.Value
                txtowner.ReadOnly = True
                ddlasset_status.Enabled = False

            End If
            ddlasset_status.SelectedValue = "Distributed"
            ddlAssetDef.DataBind()

            '如果不是代理人的權限則不需要再帶入其他資料

            If Not Request.QueryString("UID") = Nothing Then
                If hiUID.Value <> Request.QueryString("UID") Then
                    Dim uBBL As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()
                    If uBBL.ChkVIPAgend(Request.QueryString("UID").ToString, hiUID.Value, hiDBName.Value) = False Then
                        ShowMessageForAjax(btnApply, GetLocalResourceObject("NoAgent"))
                        Exit Sub
                    End If


                End If

            End If



            If Not Request.QueryString("UID") = Nothing Then ''被代理人或者是UserID
                txtowner.Text = Request.QueryString("UID")
            End If
            If Not Request.QueryString("AssetType") = Nothing Then '資產類別

                ddlAssetDef.SelectedValue = Request.QueryString("AssetType")
            End If
            If Not Request.QueryString("Asset_serialNo") = Nothing Then ''資產編號
                If bbl.GroupMemberIsExist("Admin", hiUID.Value, hiDBName.Value) Then
                    ddlasset_status.SelectedValue = ""
                End If
                txtAsset_SerialNo.Text = Request.QueryString("Asset_serialNo")
            End If

            If Not Request.QueryString("Type") = Nothing Then '回報
                If Request.QueryString("Type") = "Reply" Then
                    btnClick(intReply)

                    AssetReply1.IniData(ddlAssetDef.SelectedValue, txtAsset_SerialNo.Text, "1", hiDBName.Value, txtowner.Text)
                    AssetReply1.Visible = True
                    AssetReply1.Show()

                End If

            End If

            If Not Request.QueryString("Type") = Nothing Then '取消回報
                If Request.QueryString("Type") = "Cancel" Then
                    btnClick(intReply)

                    AssetReply1.IniData(ddlAssetDef.SelectedValue, txtAsset_SerialNo.Text, "2", hiDBName.Value, txtowner.Text)
                    AssetReply1.Visible = True
                    AssetReply1.Show()

                End If

            End If
        End If

        Me.Form.Enctype = "multipart/form-data"

    End Sub


    Protected Sub btnMaintainDetil_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        btnClick(intMaintain)
        Me.AssetMaintain1.InitDataAndShow(CType(sender, LinkButton).CommandArgument.ToString(), ChkProgramRight("2"))
    End Sub

    Private Sub btnClick(ByVal btnIndex As Integer)
        txtMaintain.BackColor = Drawing.Color.White
        txtEliminate.BackColor = Drawing.Color.White
        txtTransfer.BackColor = Drawing.Color.White
        txtFlowER.BackColor = Drawing.Color.White
        txtSOP.BackColor = Drawing.Color.White
        txtReply.BackColor = Drawing.Color.White
        Select Case btnIndex
            Case intMaintain
                txtMaintain.BackColor = Drawing.Color.Red

            Case intEliminate
                txtEliminate.BackColor = Drawing.Color.Red

            Case intTransfer
                txtTransfer.BackColor = Drawing.Color.Red

            Case intFlower
                txtFlowER.BackColor = Drawing.Color.Red
            Case intReply
                txtReply.BackColor = Drawing.Color.Red

            Case Else
                txtSOP.BackColor = Drawing.Color.Red
        End Select



    End Sub

    Protected Sub btnMaintain_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        btnClick(intMaintain)
    End Sub

    Protected Sub btnEliminate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        btnClick(intEliminate)

    End Sub

    Protected Sub btnReply_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReply.Click
        btnClick(intReply)

        AssetReply1.IniData(ddlAssetDef.SelectedValue, txtAsset_SerialNo.Text, "1", hiDBName.Value, txtowner.Text)
        AssetReply1.Visible = True
        AssetReply1.Show()


    End Sub

    Protected Sub btnSOP_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSOP.Click
        AssetHelp1.hiUID = hiUID.Value
        AssetHelp1.dbName = hiDBName.Value
        AssetHelp1.InitDataAndShow()
        btnClick(intSOP)
    End Sub


    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        btnClick(intMaintain)
        gvMain.DataBind()
    End Sub

    Protected Sub btnApply_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply.Click
        btnClick(intEliminate)
        'Wizard1.CssClass = "plUserControl"
        'Wizard1.ActiveStepIndex = 0
        AssetEliminate1.IniData(ddlAssetDef.SelectedValue, txtDept.Text, txtAsset_SerialNo.Text, hiDBName.Value, txtowner.Text)
        AssetTransfer1.IniData(ddlAssetDef.SelectedValue, txtAsset_SerialNo.Text, hiDBName.Value, txtowner.Text)
        AssetEliminate1.myOtherDT = AssetTransfer1.myDT
        AssetEliminate1.Visible = True

        mpEliminate.Show()

        ''

    End Sub

    'Protected Sub Wizard1_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles Wizard1.FinishButtonClick

    'End Sub

    'Protected Sub Wizard1_ActiveStepChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Wizard1.ActiveStepChanged
    '    Select Case Wizard1.ActiveStepIndex
    '        Case 0
    '            btnClick(intEliminate)
    '            mpSearch.Show()
    '        Case 1

    '            btnClick(intTransfer)
    '            mpSearch.Show()

    '        Case Else

    '            btnClick(intFlower)
    '            mpSearch.Show()


    '    End Select
    'End Sub

    Protected Sub OnShow(ByVal sender As Object, ByVal e As EventArgs) Handles AssetEliminate1.onShow

        mpEliminate.Show()
        btnClick(intEliminate)
    End Sub


    Protected Sub OnShowNext(ByVal sender As Object, ByVal e As EventArgs) Handles AssetEliminate1.onShowNext
        AssetTransfer1.Visible = True
        AssetTransfer1.myOtherDT = AssetEliminate1.myDT
        mpTransfer.Show()
        btnClick(intTransfer)

    End Sub

    Protected Sub OnShowTransfer(ByVal sender As Object, ByVal e As EventArgs) Handles AssetTransfer1.onShow
        mpTransfer.Show()
        btnClick(intTransfer)
    End Sub
    Protected Sub OnShowNextTransfer(ByVal sender As Object, ByVal e As EventArgs) Handles AssetTransfer1.onShowNext
        'Wizard1.ActiveStepIndex = 2
        Dim tmpDT As DataTable
        tmpDT = AssetEliminate1.myDT.Copy()
        tmpDT.Merge(AssetTransfer1.myDT)

        AssetFlowER1.myDT = tmpDT
        mpFlowER.Show()
        btnClick(intFlower)

    End Sub

    Protected Sub OnShowPreviouseTransfer(ByVal sender As Object, ByVal e As EventArgs) Handles AssetTransfer1.onShowPreviouse
        'Wizard1.ActiveStepIndex = 0
        mpEliminate.Show()
        btnClick(intEliminate)
    End Sub

    Protected Sub OnShowFlowERFinish(ByVal sender As Object, ByVal e As EventArgs) Handles AssetFlowER1.onShowFinish
        'Wizard1.ActiveStepIndex = 2
        ' mpFlowER.Show()
        btnClick(intFlower)
    End Sub
    Protected Sub OnShowFlowERPreviouse(ByVal sender As Object, ByVal e As EventArgs) Handles AssetFlowER1.onShowPreviouse
        'Wizard1.ActiveStepIndex = 1
        Dim gv As GridView = sender

        Dim tmpdt As DataTable = AssetTransfer1.myDT.Copy()
        Dim drs() As DataRow
        For Each gr As GridViewRow In gv.Rows
            drs = tmpdt.Select("asset_serialNo='" + gr.Cells(3).Text + "'")
            For Each dr As DataRow In drs
                dr("NewOwner") = CType(gr.FindControl("txtNewOwner"), TextBox).Text
                dr("NewUser2") = CType(gr.FindControl("txtNewUser2"), TextBox).Text
                dr("OtherUser") = CType(gr.FindControl("txtOtherUser"), TextBox).Text
            Next
        Next


        AssetTransfer1.myDT = tmpdt.Copy()
        AssetTransfer1.FreshData()
        mpTransfer.Show()
        btnClick(intTransfer)
    End Sub

    Protected Sub OnSave(ByVal sender As Object, ByVal e As EventArgs) Handles AssetMaintain1.onSave
        gvMain.DataBind()
        ShowMessageForAjax(btnApply, GetLocalResourceObject("Finish"))
    End Sub
    Protected Sub OnShowFlower(ByVal sender As Object, ByVal e As EventArgs) Handles AssetFlowER1.onShow
        'Wizard1.ActiveStepIndex = 0
        mpFlowER.Show()
        btnClick(intMaintain)
    End Sub
    Protected Sub OnDeleteDataFlower(ByVal sender As Object, ByVal e As EventArgs) Handles AssetFlowER1.DeleteData

        Dim asset_serialNo As String = CType(sender, LinkButton).CommandArgument.ToString
        Dim tmpdt As DataTable = AssetTransfer1.myDT.Copy()
        Dim drs() As DataRow
        drs = tmpdt.Select("asset_serialNo='" + asset_serialNo + "'")
        For Each dr As DataRow In drs
            dr.Delete()
        Next
        AssetTransfer1.myDT = tmpdt.Copy()

        tmpdt = AssetTransfer1.myOtherDT.Copy()
        drs = tmpdt.Select("asset_serialNo='" + asset_serialNo + "'")
        For Each dr As DataRow In drs
            dr.Delete()
        Next
        AssetTransfer1.myOtherDT = tmpdt.Copy()

        tmpdt = AssetEliminate1.myDT.Copy()
        drs = tmpdt.Select("asset_serialNo='" + asset_serialNo + "'")
        For Each dr As DataRow In drs
            dr.Delete()
        Next
        AssetEliminate1.myDT = tmpdt.Copy()



        tmpdt = AssetEliminate1.myOtherDT.Copy()
        drs = tmpdt.Select("asset_serialNo='" + asset_serialNo + "'")
        For Each dr As DataRow In drs
            dr.Delete()
        Next
        AssetEliminate1.myOtherDT = tmpdt.Copy()


        mpFlowER.Show()
        btnClick(intMaintain)
    End Sub




    Protected Sub OnDeleteAllDataFlower(ByVal sender As Object, ByVal e As EventArgs) Handles AssetFlowER1.DeleteAllData

        
        AssetTransfer1.RemoveData()
        AssetEliminate1.RemoveData()



       
    End Sub

    Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        btnClick(intMaintain)
        Me.AssetMaintain1.InitDataAndShow("", True)
    End Sub
End Class
